<template>
	<view class="register-container">
		<!-- 顶部返回 -->
		<view class="nav-bar">
			<view class="back-btn" @click="goBack">
				<uni-icons type="arrow-left" size="24"></uni-icons>
			</view>
			<text class="text-h2">注册账号</text>
		</view>

		<!-- 注册表单 -->
		<view class="register-form animate-slide-in">
			<view class="card shadow">
				<view class="input-group">
					<view class="input-item">
						<uni-icons type="person" size="20" ></uni-icons>
						<input type="text" v-model="form.username" placeholder="请输入用户名" placeholder-class="placeholder" />
					</view> 
					<view class="input-item">
						<uni-icons type="phone" size="20" ></uni-icons>
						<input type="number" v-model="form.phone" placeholder="请输入手机号" placeholder-class="placeholder" maxlength="11" />
					</view>
					<view class="input-item">
						<uni-icons type="locked" size="20" ></uni-icons>
						<input :type="showPassword ? 'text' : 'password'" v-model="form.password" placeholder="请设置密码" placeholder-class="placeholder" />
						<view class="password-icon" @click="togglePasswordVisibility">
							<uni-icons :type="showPassword ? 'eye-filled' : 'eye-slash-filled'" size="20" ></uni-icons>
						</view>
					</view>
					<view class="input-item verification">
						<uni-icons type="safety" size="20" ></uni-icons>
						<input type="number" v-model="form.code" placeholder="请输入验证码" placeholder-class="placeholder" maxlength="6" />
						<button class="btn btn-outline verify-btn" :class="{ disabled: counting }" @click="getVerifyCode">
							{{ counting ? `${countdown}s后重试` : '获取验证码' }}
						</button>
					</view>
				</view>

				<view class="agreement flex mt-2">
					<checkbox :checked="agreed" @tap="toggleAgreement"  style="transform:scale(0.7)" />
					<text class="text-muted">
						我已阅读并同意<text class="text-primary" @click="viewAgreement">《用户协议》</text>和<text class="text-primary" @click="viewPrivacy">《隐私政策》</text>
					</text>
				</view> 

				<button class="btn btn-primary w-full mt-3" @click="handleRegister" :loading="loading" :disabled="!agreed">
					{{ loading ? '注册中...' : '注册' }}
				</button>

				<view class="login-link text-center mt-3">
					<text class="text-muted">已有账号？</text>
					<text class="text-primary" @click="goToLogin">立即登录</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					username: '',
					phone: '',
					password: '',
					code: ''
				},
				showPassword: false,
				agreed: false,
				loading: false,
				counting: false,
				countdown: 60
			}
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			togglePasswordVisibility() {
				this.showPassword = !this.showPassword
			},
			toggleAgreement() {
				this.agreed = !this.agreed
			},
			getVerifyCode() {
				if (this.counting) return
				
				if (!this.form.phone || !/^1\d{10}$/.test(this.form.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return
				}
				
				this.counting = true
				this.countdown = 60
				
				// 发送验证码请求
				console.log('发送验证码到:', this.form.phone)
				
				const timer = setInterval(() => {
					this.countdown--
					if (this.countdown <= 0) {
						clearInterval(timer)
						this.counting = false
					}
				}, 1000)
			},
			handleRegister() {
				if (!this.form.username || !this.form.phone || !this.form.password || !this.form.code) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none'
					})
					return
				}
				
				if (!this.agreed) {
					uni.showToast({
						title: '请同意用户协议和隐私政策',
						icon: 'none'
					})
					return
				}
				
				this.loading = true
				// 模拟注册请求
				setTimeout(() => {
					this.loading = false
					uni.showToast({
						title: '注册成功',
						icon: 'success'
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				}, 1500)
			},
			viewAgreement() {
				// 查看用户协议
				uni.navigateTo({
					url: '/pages/agreement/index'
				})
			},
			viewPrivacy() {
				// 查看隐私政策
				uni.navigateTo({
					url: '/pages/privacy/index'
				})
			},
			goToLogin() {
				uni.navigateTo({
					url:'/pages/login/login'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.register-container {
		min-height: 100vh;
		padding: 0 40rpx;
		background: linear-gradient(135deg, var(--bg-color) 0%, #ffffff 100%);
	}

	.nav-bar {
		display: flex;
		align-items: center;
		height: 88rpx;
		margin-top: var(--status-bar-height);
		
		.back-btn {
			width: 88rpx;
			height: 88rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-left: -20rpx;
			border-radius: 50%;
			transition: background-color 0.3s ease;
			
			&:active {
				background-color: rgba(0, 0, 0, 0.05);
			}
		}
		
		.text-h2 {
			flex: 1;
			text-align: center;
			margin-right: 88rpx;
			margin-bottom: 0;
		}
	}

	.register-form {
		margin-top: 60rpx;
		
		.input-group {
			margin-bottom: 30rpx;
		}

		.input-item {
			display: flex;
			align-items: center;
			padding: 16rpx 20rpx;
			border-bottom: 2rpx solid var(--border-color);
			
			&:last-child {
				border-bottom: none;
			}
			
			&.verification {
				.verify-btn {
					padding: 8rpx 24rpx;
					height: auto;
					line-height: normal;
					font-size: 26rpx;
					
					&.disabled {
						opacity: 0.5;
						cursor: not-allowed;
					}
				}
			}
			
			input {
				flex: 1;
				margin-left: 20rpx;
				font-size: 28rpx;
				color: var(--text-primary);
				background: transparent;
				border: none;
				height: 60rpx;
				
				&:focus {
					box-shadow: none;
				}
			}
			
			.password-icon {
				padding: 10rpx;
			}
		}

		.placeholder {
			color: var(--text-muted);
			font-size: 28rpx;
		}
	}

	.agreement {
		padding: 0 10rpx;
		font-size: 26rpx;
		
		.text-primary {
			margin: 0 4rpx;
		}
	}

	.w-full {
		width: 100%;
	}

	.text-center {
		text-align: center;
	}

	.login-link {
		.text-primary {
			margin-left: 8rpx;
		}
	}
</style> 